package com.tdam2011.grupo04;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

@SuppressLint("CommitPrefEdits")
public class Configuracion extends Activity implements OnClickListener{
	private CheckBox chb_todos;
	private CheckBox chb_soloTelefonos;
	private CheckBox chb_soloEmail;
	private CheckBox chb_soloSmsWeb;
	
	private Button btn_aceptar;
	private Button btn_cancelar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.configuracion);
        
        chb_todos= (CheckBox) findViewById(R.id.chb_todosLosContactos);
        chb_soloTelefonos= (CheckBox) findViewById(R.id.chb_soloTelefono);
        chb_soloEmail= (CheckBox) findViewById(R.id.chb_soloEmail);
        chb_soloSmsWeb= (CheckBox) findViewById(R.id.chb_soloSmsWeb);
        
        chb_todos.setOnClickListener(this);
        chb_soloTelefonos.setOnClickListener(this);
        chb_soloEmail.setOnClickListener(this);
        chb_soloSmsWeb.setOnClickListener(this);
        
        cargarPreferencias();
        
        btn_aceptar= (Button) findViewById(R.id.btn_aceptarConfiguracion);
        btn_aceptar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View paramView) {
				SharedPreferences app_preferences = getSharedPreferences("opVisualizarContactos", MODE_PRIVATE); 
		        SharedPreferences.Editor editor = app_preferences.edit();
		        editor.putBoolean("todos", chb_todos.isChecked());
		        editor.putBoolean("telefono", chb_soloTelefonos.isChecked());
		        editor.putBoolean("mail", chb_soloEmail.isChecked());
		        editor.putBoolean("smsWeb", chb_soloSmsWeb.isChecked());
		        editor.commit();
		        
		        finish();
			}
		});
        
        btn_cancelar= (Button) findViewById(R.id.btn_cancelarConfiguracion);
        btn_cancelar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View paramView) {
		        finish();
			}
		});
	}

	@Override
	public void onClick(View paramView) {
		if(chb_todos.isPressed()){
			chb_todos.setChecked(true);
        	chb_soloTelefonos.setChecked(false);
        	chb_soloEmail.setChecked(false);
        	chb_soloSmsWeb.setChecked(false);
        	chb_todos.setChecked(true);
        }
        
        if((chb_soloTelefonos.isPressed() || chb_soloTelefonos.isPressed() || chb_soloSmsWeb.isPressed()) && chb_todos.isChecked())
        	chb_todos.setChecked(false);
	}
	
	private void cargarPreferencias(){
		SharedPreferences op_preferences = getSharedPreferences("opVisualizarContactos", MODE_PRIVATE);
        boolean VER_TODOS= op_preferences.getBoolean("todos", true);
        boolean VER_TELEFONO= op_preferences.getBoolean("telefono", false);
        boolean VER_EMAIL= op_preferences.getBoolean("mail", false);
        boolean VER_SMS_WEB= op_preferences.getBoolean("smsWeb", false);
        
        chb_todos.setChecked(VER_TODOS);
        chb_soloTelefonos.setChecked(VER_TELEFONO);
    	chb_soloEmail.setChecked(VER_EMAIL);
    	chb_soloSmsWeb.setChecked(VER_SMS_WEB);
	}
}
